<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Chapter extends CI_Controller {
    
    private $data = array();

    public function  __construct() {
        parent::__construct();

        //check session
        if( !$this->session->userdata('isLoggedIn') ) {
            redirect(base_url().'admin', 'refresh');
        }

        //Load Helper and library
        $this->load->helper("url");
        $this->load->library("ci_smarty");
        $this->load->helper("text"); 
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');
        $this->load->library('pagination'); 
        
        //$this->output->enable_profiler(TRUE);

        //Load Model
        $this->load->Model("admin/Mbook");
        $this->load->Model("admin/Mvolume");
        $this->load->Model("admin/Mchapter");

        $this->data['nameAdmin'] = $this->session->userdata('name');
    }
/*
    public function listChapter($curPage = 1 )
    {
        // load book
        $this->data['listBook'] = $this->Mbook->getBook();

        // config page
        $bookPageConfig['base_url']      = base_url("admin/list-chapter/");
        $bookPageConfig['total_rows']    = $this->Mchapter->countChapter();
        $bookPageConfig['per_page']      = 50;
        $bookPageConfig['uri_segment']   = 3;
        $bookPageConfig['cur_page']      = $curPage;

        $curOffset = $bookPageConfig['per_page'] * ($curPage - 1);

        $this->pagination->initialize($bookPageConfig);

        $this->data['pageList'] = $this->pagination->create_links();

        $this->data['listChapter'] = $this->Mchapter->getChapterByLimit($bookPageConfig['per_page'],$curOffset);
        

        $this->data['sub_selected_cate'] = 'list_chapter';
		
        $this->ci_smarty->view('backend/list-chapter.tpl',$this->data); 
    }
 */   
	public function listChapter($bookID = 0, $curPage = 1 )
    {
        // load book
        $this->data['listBook'] = $this->Mbook->getBook();

        $book_id = $this->input->post('chapterBook');
        $this->session->set_userdata('bookIdSess', $book_id);
        
        // config page
        if(isset($bookID) && $bookID > 0)
        {
            $bookPageConfig['base_url']      = base_url("admin/list-chapter/".$bookID."/");
        
            $bookPageConfig['total_rows']    = $this->Mchapter->countChapterByBookID($bookID);
            $bookPageConfig['per_page']      = 50;
            $bookPageConfig['uri_segment']   = 4;
            $bookPageConfig['cur_page']      = $curPage;
    
            $curOffset = $bookPageConfig['per_page'] * ($curPage - 1);
    
            $this->pagination->initialize($bookPageConfig);
    
            $this->data['pageList'] = $this->pagination->create_links();
    
            $this->data['listChapter'] = $this->Mchapter->getChapterByBookIDByLimit($bookID, $bookPageConfig['per_page'],$curOffset);
            
            $this->data['book_selected'] = $bookID;
        }else {
            $bookPageConfig['base_url']      = base_url("admin/list-chapter/".$book_id."/");
            
            $bookPageConfig['total_rows']    = $this->Mchapter->countChapterByBookID($book_id);
            $bookPageConfig['per_page']      = 50;
            $bookPageConfig['uri_segment']   = 4;
            $bookPageConfig['cur_page']      = $curPage;
    
            $curOffset = $bookPageConfig['per_page'] * ($curPage - 1);
    
            $this->pagination->initialize($bookPageConfig);
    
            $this->data['pageList'] = $this->pagination->create_links();
    
            $this->data['listChapter'] = $this->Mchapter->getChapterByBookIDByLimit($book_id, $bookPageConfig['per_page'],$curOffset);
            
            $this->data['book_selected'] = $book_id;
        }
        $this->data['sub_selected_cate'] = 'list_chapter';
		
        $this->ci_smarty->view('backend/list-chapter.tpl',$this->data); 
    }
    
	public function listChapterByNo()
    {
        $sessionBookID = $this->session->userdata('bookIdSess');
        // load book
        $this->data['listBook'] = $this->Mbook->getBook();
        
        $no = $this->input->post('chapterNo');
        
        if(isset($sessionBookID)){
            $this->data['listChapter'] = $this->Mchapter->getChapterByBookIDByNo($sessionBookID, $no);
        }else{
            $this->data['listChapter'] = $this->Mchapter->getChapterByNo($no);
        }      
                
        $this->data['sub_selected_cate'] = 'list_chapter';
        $this->data['chapter_no'] = $no;
		
        $this->ci_smarty->view('backend/list-chapter.tpl',$this->data); 
    }
    
    public function addChapter()
    {
        // load book
        $this->data['listBook'] = $this->Mbook->getBook();

        $this->data['listVolume'] = $this->Mvolume->getVolume();

        // precess inssert
        if($this->input->post()){
            $data = $this->input->post();
            $this->Mchapter->insertChapter($data); 
            redirect(base_url()."admin/list-chapter.html"); 
        }

        $this->data['sub_selected_cate'] = 'add_chapter';

        $this->ci_smarty->view('backend/add-chapter.tpl',$this->data); 
    }

    public function updateChapter($no, $uname)
    {
        // load chapter
        $this->data['chapter'] = $this->Mchapter->getChapterByNoByUname($no, $uname);

        //load book 
        $this->data['listBook'] = $this->Mbook->getBook();

        //load Volume 
        $this->data['listVolume'] = $this->Mvolume->getVolume();

        // process update
        if($this->input->post()){
            $data = $this->input->post();
            $this->Mchapter->updateChapter($data); 
            redirect(base_url()."admin/list-chapter.html"); 
        }
       
        $this->ci_smarty->view('backend/update-chapter.tpl',$this->data); 
    }

    // set lại chaterNo
    public function updateChapterNo()
    {
        $arrChap = $this->Mchapter->getChapterByBookID();
        $i = 1; //Hoac = 0 nếu có chương giới thiệu
        foreach ($arrChap as $chap) {
            $param['id'] = $chap['id'];
            $param['no'] = $i;
            $this->Mchapter->updateChapterNo($param);
            $i++;
        }
    }

    public function deleteChapter($no, $uname)
    {
        $this->Mchapter->deleteChapter($no, $uname); 
        redirect(base_url()."admin/list-chapter.html"); 
    }
    
    // Table Chapter Error
    
    public function listChapterError()
    {
        $this->data['listChapterError'] = $this->Mchapter->getChapterError();
		
        $this->ci_smarty->view('backend/list-chapter-error.tpl',$this->data); 
    }
    
    public function deleteChapterError($chapterErrorID)
    {
        $this->Mchapter->deleteChapterError($chapterErrorID); 
        redirect(base_url()."admin/list-chapter-error.html"); 
    }
}